package io.github.jhipster.registry.web.rest;

import io.github.jhipster.config.JHipsterProperties;
import io.github.jhipster.registry.config.DefaultProfileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Resource to return information about the currently running Spring profiles.
 */
@RestController
@RequestMapping("/api")
public class ProfileInfoResource {

    private final Environment env;

    private final JHipsterProperties jHipsterProperties;

    @Value("${spring.cloud.config.server.native.search-locations:}")
    private String nativeSearchLocation;

    @Value("${spring.cloud.config.server.git.uri:}")
    private String gitUri;

    @Value("${spring.cloud.config.server.git.search-paths:}")
    private String gitSearchLocation;

    public ProfileInfoResource(Environment env, JHipsterProperties jHipsterProperties) {
        this.env = env;
        this.jHipsterProperties = jHipsterProperties;
    }

    @GetMapping("/profile-info")
    public ProfileInfoVM getActiveProfiles() {
        String[] activeProfiles = DefaultProfileUtil.getActiveProfiles(env);
        return new ProfileInfoVM(activeProfiles, getRibbonEnv(activeProfiles), nativeSearchLocation, gitUri, gitSearchLocation);
    }

    private String getRibbonEnv(String[] activeProfiles) {
        String[] displayOnActiveProfiles = jHipsterProperties.getRibbon().getDisplayOnActiveProfiles();
        if (displayOnActiveProfiles == null) {
            return null;
        }
        List<String> ribbonProfiles = new ArrayList<>(Arrays.asList(displayOnActiveProfiles));
        List<String> springBootProfiles = Arrays.asList(activeProfiles);
        ribbonProfiles.retainAll(springBootProfiles);
        if (!ribbonProfiles.isEmpty()) {
            return ribbonProfiles.get(0);
        }
        return null;
    }

    class ProfileInfoVM {

        private String[] activeProfiles;

        private String ribbonEnv;

        private String nativeSearchLocation;

        private String gitUri;

        private String gitSearchLocation;

        ProfileInfoVM(String[] activeProfiles, String ribbonEnv, String nativeSearchLocation, String gitUri,
                      String gitSearchLocation) {
            this.activeProfiles = activeProfiles;
            this.ribbonEnv = ribbonEnv;
            this.nativeSearchLocation = nativeSearchLocation;
            this.gitUri = gitUri;
            this.gitSearchLocation = gitSearchLocation;
        }

        public String[] getActiveProfiles() {
            return activeProfiles;
        }

        public String getRibbonEnv() {
            return ribbonEnv;
        }

        public String getNativeSearchLocation() {
            return nativeSearchLocation;
        }

        public String getGitUri() {
            return gitUri;
        }

        public String getGitSearchLocation() {
            return gitSearchLocation;
        }
    }
}
